extends Node2D

var cyclonite = load("res://Assets/NPC/Main/cyclonite.png")

signal MissKill
signal Killed

# 保存上次播放动画的名称
var prev_animation

# 喊话内容
const yell_content = [
	"丑八怪啊咦呀～咦呀～",
	"我从未见过如此厚颜无耻之徒",
	"大西瓜！",
	"平角裤，平角裤",
	"木大木大木大木大",
	"giao里giao气",
]

func _ready():
	$AnimationPlayer.connect("animation_finished", self, "on_animation_finished")

# 播放出洞
func come_out():
	$AnimationPlayer.play("ChuDong") 

# 播放进洞
func come_in():
	$dialog.visible = false
	$AnimationPlayer.play("JinDong")

# 播放喊话
func __yell(text):
	$dialog/Label.text = text
	$AnimationPlayer.play("HanHua")

# 没有播放动画，视为空闲
func is_idle():
	return !$AnimationPlayer.is_playing()
	
func _on_Area2D_mouse_entered():
	Input.set_custom_mouse_cursor(cyclonite, 0, Vector2(cyclonite.get_width()/2, cyclonite.get_height()/2))

func _on_Area2D_mouse_exited():
	Input.set_custom_mouse_cursor(null)

func _on_Area2D_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton && prev_animation != "XiaoShi":
		$Mouse01/Area2D/CollisionPolygon2D.disabled = true
		emit_signal("Killed")
		
		# 清理之前播放状态
		$AnimationPlayer.clear_caches()
		$AnimationPlayer.clear_queue()
		$AnimationPlayer.stop(true)
		# 获取爆炸位置
		$explosion.position = get_local_mouse_position()
		$AnimationPlayer.play("XiaoShi")
		# 播放爆炸、尖叫音效
		$AnimationPlayer2.stop(true)
		$AnimationPlayer2.play("BoomCry")

# 重置属性
func __reset():
	$Mouse01.position = Vector2(512, 540)
	$Mouse01.region_rect = Rect2(0, 0, 480, 80)
	$Mouse01/Area2D/CollisionPolygon2D.disabled = true
	$explosion.visible = false
	
func on_animation_finished(anim_name):
	if anim_name == "XiaoShi":
		__reset()
	elif anim_name == "JinDong":
			emit_signal("MissKill")
	elif anim_name == "ChuDong":
		__yell(yell_content[randi()%len(yell_content)])
	prev_animation = anim_name